package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //菜品是否启用
        for (Long id : ids) {
            Dish dish=dishMapper.selectById(id);
            if(dish.getStatus()== StatusConstant.ENABLE)
                //菜品为起售状态 不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //菜品是否关联套餐
        for (Long id : ids) {
            SetmealDish setmealDish=setmealDishMapper.selectByDishId(id);
            if (setmealDish!=null)
            //当前菜品关联了套餐 不能删除
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //删除菜品
        dishMapper.deleteByIdBatch(ids);

        //删除菜品关联的口味
        dishFlavorMapper.deleteByDishIdBatch(ids);

    }

    @Override
    @Transactional//事务 保持数据一致
    public void insertDishWithFlavor(DishDTO dishDTO) {
        //向菜品表中添加一条数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        long id=dish.getId();
        //向口味表中添加n条数据
        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null && list.size()>0)
        {
            for (DishFlavor dishFlavor : list) {
                dishFlavor.setDishId(id);

            }
            dishFlavorMapper.insertBatch(list);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //获取分页对象
        Page<DishVO> page=dishMapper.page(dishPageQueryDTO);
        long total=page.getTotal();
        List<DishVO> list=page.getResult();
        return new PageResult(total,list);
    }

    @Override
    public DishVO selectByIdFromDishAndFlavor(Long id) {
        //查询菜品
        Dish dish=dishMapper.selectById(id);

        //查询口味
        List<DishFlavor> list=dishFlavorMapper.selectByDishId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDto) {
        //修改菜品信息
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dishMapper.update(dish);
        //修改口味信息
        //1删除原有口味信息
        Long id=dish.getId();
        List ids=new ArrayList<>();
        ids.add(id);
        dishFlavorMapper.deleteByDishIdBatch(ids);
        //2添加新的口味信息
        List<DishFlavor> list=dishDto.getFlavors();
        if(list!=null && list.size()>0)
        {
            for (DishFlavor dishFlavor : list) {
                dishFlavor.setDishId(id);

            }
            dishFlavorMapper.insertBatch(list);
        }
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 菜品起售停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }



}
